home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / pdcurs22 / samples / tui.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  18.6 KB  |  738 lines

  1. /*
  2. ***************************************************************************
  3. * This file comprises part of PDCurses. PDCurses is Public Domain software.
  4. * You may use this code for whatever purposes you desire. This software
  5. * is provided AS IS with NO WARRANTY whatsoever.
  6. * Should this software be used in another application, an acknowledgement
  7. * that PDCurses code is used would be appreciated, but is not mandatory.
  8. *
  9. * Any changes which you make to this software which may improve or enhance
  10. * it, should be forwarded to the current maintainer for the benefit of 
  11. * other users.
  12. *
  13. * The only restriction placed on this code is that no distribution of
  14. * modified PDCurses code be made under the PDCurses name, by anyone
  15. * other than the current maintainer.
  16. * See the file maintain.er for details of the current maintainer.
  17. ***************************************************************************
  18. */
  19. /********************************* tui.c ************************************/
  20. /*
  21.  * File   : tui.c      'textual user interface'
  22.  * Author : P.J. Kunst  (kunst@prl.philips.nl)
  23.  * Date   : 25-02-93
  24.  * Version: 1.02
  25.  */
  26.  
  27. #include <ctype.h>
  28. #include <curses.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include "tui.h"
  34.  
  35. #ifdef PDCDEBUG
  36. char *rcsid_tui  = "$Id$";
  37. #endif
  38.  
  39. #if defined(__unix) && !defined(GO32)
  40. #define CPUACCOUNT                /* necessary if CPU cycles are to be paid! */
  41. #endif
  42.  
  43. #ifdef A_COLOR
  44. # define TITLECOLOR         1                   /* color pair indices */
  45. # define MAINMENUCOLOR      (2 | A_BOLD)
  46. # define MAINMENUREVCOLOR   (3 | A_BOLD | A_REVERSE)
  47. # define SUBMENUCOLOR       (4 | A_BOLD)
  48. # define SUBMENUREVCOLOR    (5 | A_BOLD | A_REVERSE)
  49. # define BODYCOLOR          6
  50. # define STATUSCOLOR        (7 | A_BOLD)
  51. # define INPUTBOXCOLOR      8
  52. # define EDITBOXCOLOR       (9 | A_BOLD | A_REVERSE)
  53. #else
  54. # define TITLECOLOR         0                   /* color pair indices */
  55. # define MAINMENUCOLOR      (A_BOLD)
  56. # define MAINMENUREVCOLOR   (A_BOLD | A_REVERSE)
  57. # define SUBMENUCOLOR       (A_BOLD)
  58. # define SUBMENUREVCOLOR    (A_BOLD | A_REVERSE)
  59. # define BODYCOLOR          0
  60. # define STATUSCOLOR        (A_BOLD)
  61. # define INPUTBOXCOLOR      0
  62. # define EDITBOXCOLOR       (A_BOLD | A_REVERSE)
  63. #endif
  64.  
  65.  
  66. #define  th   1                   /* title window height */
  67. #define  mh   1                   /* main menu height */
  68. #define  sh   2                   /* status window height */
  69. #define  bh   (LINES-th-mh-sh-1)  /* body window height */
  70. #define  bw   COLS                /* body window width */
  71.  
  72.  
  73. /******************************* STATIC ************************************/
  74.  
  75. static WINDOW *wtitl, *wmain, *wbody, *wstat; /* title, menu, body, status win*/
  76. static int nexty, nextx;
  77. static int key = ERR, ch = ERR;
  78. static bool quit = FALSE;
  79. static bool incurses = FALSE;
  80.  
  81. #ifndef PDCURSES
  82. static char wordchar (void) { return 0x17; }  /* ^W */ 
  83. #endif
  84.  
  85. static char *padstr (char *s, int length)
  86. {
  87.   static char buf[MAXSTRLEN];
  88.   char fmt[10];
  89.  
  90.   sprintf (fmt, strlen(s)>length ? "%%.%ds" : "%%-%ds", length);
  91.   sprintf (buf, fmt, s);
  92.   return buf;
  93. }
  94.  
  95. static char *prepad (char *s, int length)
  96. {
  97.   int i;
  98.   char *p = s;
  99.  
  100.   if (length > 0)
  101.   {
  102.     memmove ((void *)(s+length), (const void *)s, strlen(s)+1);
  103.     for (i=0; i<length; i++) *p++ = ' ';
  104.   }
  105.   return s;
  106. }
  107.  
  108. static void rmline (WINDOW *win, int nr)   /* keeps box lines intact */
  109. {
  110.   mvwaddstr (win, nr, 1, padstr(" ", bw-2));
  111.   wrefresh (win);
  112. }
  113.  
  114. static void initcolor (void)
  115. {
  116. #ifdef A_COLOR
  117.   if (has_colors()) start_color();     /* foreground, background */
  118.   init_pair (TITLECOLOR       & ~A_ATTR, COLOR_BLACK, COLOR_CYAN);      
  119.   init_pair (MAINMENUCOLOR    & ~A_ATTR, COLOR_WHITE, COLOR_CYAN);    
  120.   init_pair (MAINMENUREVCOLOR & ~A_ATTR, COLOR_WHITE, COLOR_BLACK);
  121.   init_pair (SUBMENUCOLOR     & ~A_ATTR, COLOR_WHITE, COLOR_CYAN);    
  122.   init_pair (SUBMENUREVCOLOR  & ~A_ATTR, COLOR_WHITE, COLOR_BLACK);   
  123.   init_pair (BODYCOLOR        & ~A_ATTR, COLOR_WHITE, COLOR_BLUE);      
  124.   init_pair (STATUSCOLOR      & ~A_ATTR, COLOR_WHITE, COLOR_CYAN);   
  125.   init_pair (INPUTBOXCOLOR    & ~A_ATTR, COLOR_BLACK, COLOR_CYAN);
  126.   init_pair (EDITBOXCOLOR     & ~A_ATTR, COLOR_WHITE, COLOR_BLACK);
  127. #endif
  128. }
  129.  
  130. static void setcolor (WINDOW *win, chtype color)
  131. {
  132.   chtype attr = color & A_ATTR;  /* extract Bold, Reverse, Blink bits */
  133.  
  134. #ifdef A_COLOR
  135.   attr &= ~A_REVERSE;  /* ignore reverse, use colors instead! */
  136.   wattrset (win, COLOR_PAIR(color & A_CHARTEXT) | attr);
  137. #else
  138.   attr &= ~A_BOLD;     /* ignore bold, gives messy display on HP-UX */
  139.   wattrset (win, attr);
  140. #endif
  141. }
  142.  
  143. static void colorbox (WINDOW *win, chtype color, int hasbox)
  144. {
  145.   chtype attr = color & A_ATTR;  /* extract Bold, Reverse, Blink bits */
  146.  
  147.   setcolor (win, color);
  148. #ifdef A_COLOR
  149.   if (has_colors())
  150.      wbkgd (win, COLOR_PAIR(color & A_CHARTEXT) | (attr & ~A_REVERSE));
  151.   else
  152.      wbkgd (win, color);
  153. #else
  154.   wbkgd (win, color);
  155. #endif
  156.   werase (win); 
  157.   if (hasbox && win->_maxy > 2) box (win, 0, 0);
  158.   touchwin (win); wrefresh (win);
  159. }
  160.  
  161. static void idle (void)
  162. {
  163.   char buf[MAXSTRLEN];
  164.   time_t t;
  165.   struct tm *tp;
  166.  
  167.   if (time (&t) == -1) return;  /* time not available */
  168.   tp = localtime(&t);
  169.   sprintf (buf, " %.2d-%.2d-%.2d  %.2d:%.2d:%.2d", 
  170.     tp->tm_mday, tp->tm_mon + 1, tp->tm_year,
  171.     tp->tm_hour, tp->tm_min, tp->tm_sec);
  172.   mvwaddstr (wtitl, 0, bw-strlen(buf)-2, buf);
  173.   wrefresh (wtitl); 
  174. #ifdef CPUACCOUNT
  175.   sleep (1);    /* necessary if CPU cycles are to be paid ! */
  176. #endif
  177. }
  178.  
  179. static void menudim (menu *mp, int *lines, int *columns)
  180. {
  181.   int n, l, max = 0;
  182.  
  183.   for (n=0; mp->func; n++, mp++)
  184.     if ((l = strlen(mp->name)) > max) max = l;
  185.   *lines = n;
  186.   *columns = max + 2;
  187. }
  188.  
  189. static void setmenupos (int y, int x)
  190. {
  191.   nexty = y; nextx = x;
  192. }
  193.  
  194. static void getmenupos (int *y, int *x)
  195. {
  196.   *y = nexty; *x = nextx;
  197. }
  198.  
  199. static int hotkey (char *s)
  200. {
  201.   int c, c0 = *s;  /* if no upper case found, return first char */
  202.  
  203.   for (c = *s; c; s++) if (isupper(c)) break;
  204.   return c ? c : c0;
  205. }
  206.  
  207. static void repaintmenu (WINDOW *wmenu, menu *mp)
  208. {
  209.   int i;
  210.   menu *p = mp;
  211.  
  212.   for (i=0; p->func; i++, p++)
  213.     mvwaddstr (wmenu, i+1, 2, p->name);
  214.   touchwin (wmenu); wrefresh (wmenu);
  215. }
  216.  
  217. static void repaintmainmenu (int width, menu *mp)
  218. {
  219.   int i;
  220.   menu *p = mp;
  221.  
  222.   for (i=0; p->func; i++, p++)
  223.     mvwaddstr (wmain, 0, i*width, prepad (padstr(p->name, width-1), 1));
  224.   touchwin (wmain); wrefresh (wmain);
  225. }
  226.  
  227. static void mainhelp (void)
  228. {
  229. #ifdef ALT_X
  230.   statusmsg ("Use arrow keys and Enter to select (Alt-X to quit)");
  231. #else
  232.   statusmsg ("Use arrow keys and Enter to select");
  233. #endif
  234. }
  235.  
  236. static void hidecursor (void)
  237. {
  238. #if defined(PDCURSES) || defined(SYSV)
  239.   curs_set (0);
  240. #endif
  241. }
  242.  
  243. static void normalcursor (void)
  244. {
  245. #if defined(PDCURSES) || defined(SYSV)
  246.   curs_set (1);
  247. #endif
  248. }
  249.  
  250. static void insertcursor (void)
  251. {
  252. #if defined(PDCURSES) || defined(SYSV)
  253.   curs_set (2);
  254. #endif
  255. }
  256.  
  257. static void mainmenu (menu *mp)
  258. {
  259.   int nitems, barlen, old = -1, cur = 0, c, cur0;
  260.  
  261.   menudim (mp, &nitems, &barlen);
  262.   repaintmainmenu (barlen, mp);
  263.  
  264.   while (!quit)
  265.   {
  266.     if (cur != old)
  267.     {
  268.       if (old != -1)
  269.       {
  270.         mvwaddstr (wmain, 0, old*barlen, 
  271.           prepad (padstr(mp[old].name, barlen-1), 1));
  272.         statusmsg (mp[cur].desc);
  273.       }
  274.       else mainhelp ();
  275.       setcolor (wmain, MAINMENUREVCOLOR);
  276.       mvwaddstr (wmain, 0, cur*barlen, 
  277.         prepad (padstr(mp[cur].name, barlen-1), 1));
  278.       setcolor (wmain, MAINMENUCOLOR);
  279.       old = cur;
  280.       wrefresh (wmain);
  281.     }
  282.     switch (c = (key != ERR ? key : waitforkey()))
  283.     {
  284.       case KEY_DOWN:
  285.       case '\n':                              /* menu item selected ! */
  286.         touchwin (wbody); wrefresh (wbody);
  287.         rmerror ();
  288.         setmenupos (th+mh, cur*barlen);
  289.         normalcursor ();
  290.         (mp[cur].func)();                     /* perform function */
  291.         hidecursor ();
  292.         switch (key)
  293.         {
  294.           case KEY_LEFT:
  295.             cur = (cur+nitems-1) % nitems;
  296.             key = '\n';
  297.             break;
  298.  
  299.           case KEY_RIGHT:
  300.             cur = (cur+1) % nitems;
  301.             key = '\n';
  302.             break;
  303.  
  304.           default:
  305.             key = ERR;
  306.             break;
  307.         }
  308.         repaintmainmenu (barlen, mp);
  309.         old = -1;
  310.         break;
  311.  
  312.       case KEY_LEFT:
  313.         cur = (cur+nitems-1) % nitems;
  314.         break;
  315.  
  316.       case KEY_RIGHT:
  317.         cur = (cur+1) % nitems;
  318.         break;
  319.  
  320.       case KEY_ESC:
  321.         mainhelp ();
  322.         break;
  323.  
  324.       default:
  325.         cur0 = cur;
  326.         do { cur = (cur+1) % nitems;
  327.         } while ((cur != cur0) && (hotkey(mp[cur].name) != toupper(c)));
  328.         if (hotkey(mp[cur].name) == toupper(c)) key = '\n';
  329.         break;
  330.     }
  331.   }
  332.   rmerror ();
  333.   touchwin (wbody); wrefresh (wbody);
  334. }
  335.  
  336. static void cleanup (void) /* cleanup curses settings */
  337. {
  338.   if (incurses)
  339.   {
  340.     delwin (wtitl);
  341.     delwin (wmain);
  342.     delwin (wbody);
  343.     delwin (wstat);
  344.     normalcursor ();
  345.     endwin (); 
  346.     incurses = FALSE;
  347.   }
  348. }
  349.  
  350.  
  351. /******************************* EXTERNAL **********************************/
  352.  
  353. void clsbody (void)
  354. {
  355.   werase (wbody); wmove (wbody, 0, 0);
  356. }
  357.  
  358. int bodylen (void)
  359. {
  360.   return wbody->_maxy;
  361. }
  362.  
  363. WINDOW *bodywin (void)
  364. {
  365.   return wbody;
  366. }
  367.  
  368. void rmerror (void)
  369. {
  370.   rmline (wstat, 0);
  371. }
  372.  
  373. void rmstatus (void)
  374. {
  375.   rmline (wstat, 1);
  376. }
  377.  
  378. void titlemsg (char *msg)
  379. {
  380.   mvwaddstr (wtitl, 0, 2, padstr(msg, bw-3));
  381.   wrefresh (wtitl);
  382. }
  383.  
  384. void bodymsg (char *msg)
  385. {
  386.   waddstr (wbody, msg);
  387.   wrefresh (wbody);
  388. }
  389.  
  390. void errormsg (char *msg)
  391. {
  392.   beep ();
  393.   mvwaddstr (wstat, 0, 2, padstr(msg, bw-3));
  394.   wrefresh (wstat);
  395. }
  396.  
  397. void statusmsg (char *msg)
  398. {
  399.   mvwaddstr (wstat, 1, 2, padstr(msg, bw-3));
  400.   wrefresh (wstat);
  401. }
  402.  
  403. bool keypressed (void)
  404. {
  405.   if (ch == ERR) ch = wgetch(wbody);
  406.   return ch != ERR;
  407. }
  408.  
  409. int getkey (void)
  410. {
  411.   int c = ch;
  412.  
  413.   ch = ERR;
  414. #ifdef ALT_X
  415.   quit = (c == ALT_X);     /* PC only ! */
  416. #endif
  417.   return c;
  418. }
  419.  
  420. void flushkeys (void)
  421. {
  422.   while (keypressed()) getkey();
  423. }
  424.  
  425. int waitforkey (void)
  426. {
  427.   flushkeys ();
  428.   while (!keypressed()) idle ();
  429.   return getkey ();
  430. }
  431.  
  432. void DoExit (void)   /* terminate program */
  433. {
  434.   quit = TRUE;
  435. }
  436.  
  437. void domenu (menu *mp)
  438. {
  439.   int y, x, nitems, barlen, mheight, mw, old = -1, cur = 0, cur0;
  440.   bool stop = FALSE;
  441.   WINDOW *wmenu;
  442.  
  443.   hidecursor ();
  444.   getmenupos (&y, &x);
  445.   menudim (mp, &nitems, &barlen);
  446.   mheight = nitems+2; mw = barlen+2;
  447.   wmenu = newwin (mheight, mw, y, x);
  448.   colorbox (wmenu, SUBMENUCOLOR, 1);
  449.   repaintmenu (wmenu, mp);
  450.  
  451.   key = ERR;
  452.   while (!stop && !quit)
  453.   {
  454.     if (cur != old)
  455.     {
  456.       if (old != -1)
  457.         mvwaddstr (wmenu, old+1, 1, prepad (padstr(mp[old].name, barlen-1), 1));
  458.       setcolor (wmenu, SUBMENUREVCOLOR);
  459.       mvwaddstr (wmenu, cur+1, 1, prepad (padstr(mp[cur].name, barlen-1), 1));
  460.       setcolor (wmenu, SUBMENUCOLOR);
  461.       statusmsg (mp[cur].desc);
  462.       old = cur;
  463.       wrefresh (wmenu);
  464.     }
  465.     switch (key = ((key != ERR) ? key : waitforkey()))
  466.     {
  467.       case '\n':                              /* menu item selected ! */
  468.         touchwin (wbody); wrefresh (wbody);
  469.         setmenupos (y+1, x+1);
  470.         rmerror ();
  471.         key = ERR;
  472.         normalcursor ();
  473.         (mp[cur].func)();                     /* perform function */
  474.         hidecursor ();
  475.         repaintmenu (wmenu, mp);
  476.         old = -1;
  477.         break;
  478.  
  479.       case KEY_UP:
  480.         cur = (cur+nitems-1) % nitems;
  481.         key = ERR;
  482.         break;
  483.  
  484.       case KEY_DOWN:
  485.         cur = (cur+1) % nitems;
  486.         key = ERR;
  487.         break;
  488.  
  489.       case KEY_ESC:
  490.       case KEY_LEFT:
  491.       case KEY_RIGHT:
  492.         if (key == KEY_ESC) key = ERR;  /* return to previous submenu */
  493.         stop = TRUE;
  494.         break;
  495.  
  496.       default:
  497.         cur0 = cur;
  498.         do { cur = (cur+1) % nitems;
  499.         } while ((cur != cur0) && (hotkey(mp[cur].name) != toupper((int)key)));
  500.         key = (hotkey(mp[cur].name) == toupper((int)key)) ? '\n' : ERR;
  501.         break;
  502.     }
  503.   }
  504.   rmerror ();
  505.   delwin (wmenu);
  506.   touchwin (wbody); wrefresh (wbody);
  507. }
  508.  
  509. void startmenu (menu *mp, char *title)
  510. {
  511.   initscr ();
  512.   incurses = TRUE;
  513.   initcolor ();
  514. /*  atexit (cleanup); */
  515.  
  516.   wtitl = subwin (stdscr, th,  bw,    0, 0);
  517.   wmain = subwin (stdscr, mh,  bw,   th, 0);
  518.   wbody = subwin (stdscr, bh,  bw,  th+mh, 0);
  519.   wstat = subwin (stdscr, sh,  bw, th+mh+bh, 0);
  520.  
  521.   colorbox (wtitl, TITLECOLOR, 0);
  522.   colorbox (wmain, MAINMENUCOLOR, 0);
  523.   colorbox (wbody, BODYCOLOR, 0);
  524.   colorbox (wstat, STATUSCOLOR, 0);
  525.  
  526.   if (title) titlemsg (title);
  527.  
  528.   cbreak ();                /* direct input (no newline required)... */
  529.   noecho ();                /* ... without echoing */
  530.   hidecursor ();            /* hide cursor (if possible) */
  531.   nodelay (wbody, TRUE);    /* don't wait for input */
  532.   keypad (wbody, TRUE);     /* enable cursor keys */
  533.   scrollok (wbody, TRUE);   /* enable scrolling in main window */
  534.   leaveok (stdscr, TRUE);
  535.   leaveok (wtitl, TRUE);
  536.   leaveok (wmain, TRUE);
  537.   leaveok (wstat, TRUE);
  538.  
  539.   mainmenu (mp);
  540.  
  541.   cleanup ();
  542. }
  543.  
  544.  
  545. /*man-start*********************************************************************
  546.  
  547.   weditstr()     - edit string
  548.  
  549.   PDCurses Description:
  550.         The initial value of 'str' with a maximum length of 'field'-1,
  551.         which is supplied by the calling routine, is editted. The user's 
  552.         erase (^H), kill (^U) and delete word (^W) chars are interpreted. 
  553.         The PC insert or Tab keys toggle between insert and edit mode.
  554.         Escape aborts the edit session, leaving 'str' unchanged.
  555.         Enter, Up or Down Arrow are used to accept the changes to 'str'.
  556.  
  557.         NOTE: editstr(), mveditstr(), and mvweditstr() are macros.
  558.  
  559.   PDCurses Return Value:
  560.         These functions return the input terminating character on 
  561.         success (Escape, Enter, Up or Down Arrow) and ERR on error.
  562.  
  563.   PDCurses Errors:
  564.         It is an error to call this function with a NULL window pointer.
  565.         The length of the initial 'str' must not exceed 'field'-1.
  566.  
  567.   Portability:
  568.         PDCurses        int weditstr( WINDOW* win, char* str, int field );
  569.         X/Open Dec '88 
  570.         BSD Curses    
  571.         SYS V Curses 
  572.  
  573. **man-end**********************************************************************/
  574.  
  575.  
  576. static void repainteditbox (WINDOW *win, int x, char *buf)
  577. {
  578.   werase (win); mvwprintw (win, 0, 0, "%s", padstr (buf, win->_maxx));
  579.   wmove (win, 0, x); wrefresh (win); 
  580. }
  581.  
  582. int weditstr (WINDOW *win, char *buf, int field)
  583. {
  584.   char org[MAXSTRLEN], *tp, *bp = buf;
  585.   bool defdisp = TRUE, stop = FALSE, insert = FALSE;
  586.   int cury, curx, begy, begx, oldattr;
  587.   WINDOW *wedit;
  588.   int c=0;
  589.  
  590.   if ((field>=MAXSTRLEN) || (buf==NULL) || (strlen(buf) > field-1)) return ERR;
  591.   strcpy (org, buf);  /* save original */
  592.  
  593.   wrefresh (win);
  594.   getyx (win, cury, curx); getbegyx (win, begy, begx);
  595.   wedit = subwin (win, 1, field, begy+cury, begx+curx);  /* window relative */
  596.   oldattr = wedit->_attrs;
  597.   colorbox (wedit, EDITBOXCOLOR, 0);
  598.  
  599.   normalcursor ();
  600. #ifdef CPUACCOUNT
  601.   nodelay (win, FALSE);
  602.   keypad (wedit, TRUE);
  603. #endif
  604.   while (!stop)
  605.   {
  606.     repainteditbox (wedit, bp-buf, buf);
  607. #ifdef CPUACCOUNT
  608.     switch (c = wgetch(wedit))
  609. #else
  610.     switch (c = waitforkey())
  611. #endif
  612.     {
  613.       case KEY_ESC:
  614.         strcpy (buf, org);  /* restore original */
  615.         stop = TRUE;
  616.         break;
  617.  
  618.       case '\n':  /* ENTER */
  619.       case KEY_UP:
  620.       case KEY_DOWN:
  621.         stop = TRUE;
  622.         break;
  623.  
  624.       case KEY_LEFT:
  625.         if (bp > buf) bp--;
  626.         break;
  627.  
  628.       case KEY_RIGHT:
  629.         defdisp = FALSE;
  630.         if (bp - buf < strlen(buf)) bp++;
  631.         break;
  632.  
  633.       case '\t':     /* TAB, because Insert not properly handled on HP-UX ! */
  634.       case KEY_IC:   /* enter insert mode */
  635.       case KEY_EIC:  /* exit insert mode */
  636.         defdisp = FALSE;
  637.         insert = !insert;
  638.         if (insert) insertcursor (); else normalcursor ();
  639.         break;
  640.  
  641.       default:
  642.         if (c == erasechar())  /* backspace, ^H */
  643.         {
  644.           if (bp > buf)
  645.           {
  646.             memmove ((void *)(bp-1), (const void *)bp, strlen(bp)+1);
  647.             bp--;
  648.           }
  649.         }
  650.         else if (c == killchar())  /* ^U */
  651.         {
  652.           bp = buf; *bp = '\0';
  653.         }
  654.         else if (c == wordchar())  /* ^W */
  655.         {
  656.           tp = bp;
  657.           while ((bp > buf) && (*(bp-1) == ' ')) bp--;
  658.           while ((bp > buf) && (*(bp-1) != ' ')) bp--;
  659.           memmove ((void *)bp, (const void *)tp, strlen(tp)+1);
  660.         }
  661.         else if (isprint(c))
  662.         {
  663.           if (defdisp) { bp = buf; *bp = '\0'; defdisp = FALSE; }
  664.           if (insert)
  665.           {
  666.             if (strlen(buf) < field-1)
  667.             {
  668.               memmove ((void *)(bp+1), (const void *)bp, strlen(bp)+1);
  669.               *bp++ = c;
  670.             }
  671.           }
  672.           else if (bp-buf < field-1)
  673.           {
  674.             if (!*bp) bp[1] = '\0'  /* append new string terminator */;
  675.             *bp++ = c;
  676.           }
  677.         }
  678.         break;
  679.     }
  680.   }
  681. #ifdef CPUACCOUNT
  682.   nodelay (win, TRUE);
  683. #endif
  684.   wattrset (wedit, oldattr);
  685.   repainteditbox (wedit, bp-buf, buf);
  686.   delwin (wedit);
  687.   return c;
  688. }
  689.  
  690. WINDOW *winputbox (WINDOW *win, int nlines, int ncols)
  691. {
  692.   int cury, curx, begy, begx;
  693.   WINDOW *winp;
  694.  
  695.   getyx (win, cury, curx); getbegyx (win, begy, begx);
  696.   winp = newwin (nlines, ncols, begy+cury, begx+curx); /* window relative */
  697.   colorbox (winp, INPUTBOXCOLOR, 1);
  698.   return winp;
  699. }
  700.  
  701. int getstrings (char *desc[], char *buf[], int field)
  702. {
  703.   WINDOW *winput;
  704.   int oldy, oldx, maxy, maxx, nlines, ncols, i, n, l, max = 0;
  705.   int c=0;
  706.   bool stop = FALSE;
  707.  
  708.   for (n=0; desc[n]; n++) if ((l = strlen(desc[n])) > max) max = l;
  709.   nlines = n + 2; ncols = max + field + 4;
  710.   getyx (wbody, oldy, oldx); getmaxyx (wbody, maxy, maxx);
  711.   winput = mvwinputbox (wbody, (maxy-nlines)/2, (maxx-ncols)/2, nlines, ncols);
  712.   for (i=0; i<n; i++) mvwprintw (winput, i+1, 2, "%s", desc[i]);
  713.   i = 0;
  714.   while (!stop)
  715.   {
  716.     switch (c = mvweditstr(winput, i+1, max+3, buf[i], field))
  717.     {
  718.       case KEY_ESC:
  719.         stop = TRUE;
  720.         break;
  721.  
  722.       case KEY_UP:
  723.         i = (i+n-1)%n;
  724.         break;
  725.  
  726.       case '\n':
  727.       case '\t':
  728.       case KEY_DOWN:
  729.         if (++i == n) stop = TRUE;  /* all passed ? */
  730.         break;
  731.     }
  732.   }
  733.   delwin (winput);
  734.   touchwin (wbody); wmove (wbody, oldy, oldx); wrefresh (wbody);
  735.   return c;
  736. }
  737.